// 引入接口文件
import { submit,queryInfo,verifyCodeFun } from './userDetailServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import {isEmpty, isArray, cloneDeep, merge} from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'

// 初始默认状态
const defultState = {
  modelObj: {
    btnLoading: false,
  },
  loading: false,
  actionType: 'add',
  checkAll: false,
  indeterminate:false,
  query: {
    loginName:'',
    name: '',
    remarks: '',
    status: '',
    appRoleZituo: '',
  }
}

// 声明module
const tmpModule = {
  namespace: 'defaultModel',
  // 默认状态
  state: cloneDeep(defultState),
  // 入口函数(先执行)
  subscriptions: {
    setup ({dispatch, history}) {
      history.listen(({ pathname, query }) => {
        // 页面路由匹配时执行
        if('' + tmpModule.pathname === '' + pathname){
          // 执行业务操作
          dispatch({
            type: 'queryInfo',
            payload: {query}
          }) // end dispatch
        } // end if
      }) // end listen
    },
  },

  // 方法列表
  effects: {
    // 查询记录
    *queryInfo({payload}, {put, select, call}){
      let {query} = yield select(state => state[tmpModule.namespace])
      let params = { "id":payload.query.id }
      if(yield call(verifyCodeFun,{})){
        const { success, msg, data } = yield call(queryInfo, {...params})
        if(data == "{}"){
          query = {
            loginName:'',
            name: '',
            remarks: '',
            status: '',
            appRoleZituo: '',
          }
        }else{
          if(success){
            let dat = data
            // let dat = JSON.parse(data)
            if(dat)
            query = dat.user
            yield put({
              type: 'updateStore',
              payload: {query}
            })
          }
        }
      }
    },
    *submit({payload},{put,select,call}){
      let {query} = yield select(state => state[tmpModule.namespace])
      let params = query
      const { success, msg, data } = yield call(submit, {...params})

      if(success){
        message.success('编辑成功')
        hashHistory.push({
          pathname: '/home/expandManage/userList',
          query: {}
        })
      }
    }
  },

  // 存入状态机
  reducers: {
    updateStore(preState, action) {
      return Object.assign({}, preState, action.payload)
    },
  }
}

export default tmpModule
